package com.xpgk.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.mysql.jdbc.StringUtils;
import com.xpgk.mvc.organization.Organization;

public class SerialNumberUtil {
	
	
	public  static synchronized String getSerialNumber(Organization  org,String ywxh) {
		SimpleDateFormat  sdf = new SimpleDateFormat("yyyyMMdd");
		String date = sdf.format(new Date());
		String level = org.getBigDecimal(Organization.column_level).toString();
		String name = "";
		if(level.equals("4")){
			org=Organization.dao.findById( org.getStr(Organization.column_parentIds));
			name=org.getStr(Organization.column_name);
		}else if(level.equals("3")){
			name=org.getStr(Organization.column_name);
		}
		if(!StringUtils.isNullOrEmpty(name)){
			name=name.substring(0, 2);
		}
		String  str = date+getXzqh(name)+ywxh;
		Record record =Db.findFirst("SELECT * FROM SYS_SERIALNUMBER s WHERE  s.NAME = '流水号'  AND s.IDS = '1'");
		String code = record.get("CODE");
		int  i =Integer.parseInt(code)+1;
		String  result = String.format("%06d", i);
		record.set("CODE", result);
		String id = record.getStr("ids");
		Db.update("update SYS_SERIALNUMBER set CODE = ? where IDS = ?" ,result,id);
		return str+result;
	}
	
	
	
	
	private static String getXzqh(String value){  
		Map<String,String> map = new HashMap<String,String>();
		map.put("00", "");
		map.put("01", "平城");
		map.put("02", "云冈");
		map.put("03", "新荣");
		map.put("04", "云州");
		map.put("05", "左云");
		map.put("06", "阳高");
		map.put("07", "浑源");
		map.put("08", "天镇");
		map.put("09", "广灵");
		map.put("10", "灵丘");
        String key="";  
        for (Map.Entry<String, String> entry : map.entrySet()) {  
            if(value.equals(entry.getValue())){  
                key=entry.getKey();  
            }  
        }  
        return key;  
    } 
	
	
	
	
	
	
	
	
	
	
	
	
	
	public static void main(String[] args) {/*

		String a="201805290010";
		
		int data=10001;

		String g=a.substring(0,8);
		
		int e=data+Integer.parseInt(a.substring(a.length()-4));

		String f=String.valueOf(e);

		String f1=f.substring(f.length()-4);

		System.out.println(g+f1);
	*/
		
	System.out.println(getXzqh("阳高"));
	}
}
